package com.maple.retry.retrytask.model;

import com.maple.retry.retrytask.enums.RetryTaskStatusEnum;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.util.Date;

/**
 * @author: maple
 * @version: RetryTaskInfo.java, v 0.1 2020年12月01日 17:32 maple Exp $
 */
public class RetryTaskInfo implements Serializable {

    /**
     * serial version id
     */
    private static final long serialVersionUID = -4725738143937067976L;

    /**
     * 补偿任务id
     */
    private String id;

    /**
     * 补偿任务类型,在注册新任务时由业务确认
     */
    private String bizType;

    /**
     * 补偿任务相关的业务单据id
     */
    private String bizId;

    /**
     * 业务数据,重试任务调度时返回给具体业务
     */
    private String bizContext;

    /**
     * 重试任务状态枚举
     */
    private RetryTaskStatusEnum status = RetryTaskStatusEnum.INIT;

    /**
     * 重试任务重试次数
     */
    private int retryNo;

    /**
     * 备注说明
     */
    private String memo;

    /**
     * 任务创建时间
     */
    private Date gmtCreate;

    /**
     * 任务最近一次修改时间
     */
    private Date gmtModified;

    /**
     * 任务下次调度时间
     */
    private Date scheduleTime;

    /**
     * 重试次数累加
     * 如果当前重试值等于int最大值,累加后归零
     */
    public void increaseRetryNo() {
        int oldRetryNo = Integer.valueOf(this.retryNo);
        this.retryNo = Integer.MAX_VALUE > oldRetryNo ? oldRetryNo + 1 : 0;
    }

    /**
     * 任务是否被归档
     *
     * @return 任务是否被归档
     */
    public boolean isAchieved() {
        return this.status != RetryTaskStatusEnum.INIT;
    }

    /**
     * Getter method for property id.
     *
     * @return property value of id
     */
    public String getId() {
        return id;
    }

    /**
     * Setter method for property id.
     *
     * @param id value to be assigned to property id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Getter method for property bizType.
     *
     * @return property value of bizType
     */
    public String getBizType() {
        return bizType;
    }

    /**
     * Setter method for property bizType.
     *
     * @param bizType value to be assigned to property bizType
     */
    public void setBizType(String bizType) {
        this.bizType = bizType;
    }

    /**
     * Getter method for property bizId.
     *
     * @return property value of bizId
     */
    public String getBizId() {
        return bizId;
    }

    /**
     * Setter method for property bizId.
     *
     * @param bizId value to be assigned to property bizId
     */
    public void setBizId(String bizId) {
        this.bizId = bizId;
    }

    /**
     * Getter method for property bizContext.
     *
     * @return property value of bizContext
     */
    public String getBizContext() {
        return bizContext;
    }

    /**
     * Setter method for property bizContext.
     *
     * @param bizContext value to be assigned to property bizContext
     */
    public void setBizContext(String bizContext) {
        this.bizContext = bizContext;
    }

    /**
     * Getter method for property status.
     *
     * @return property value of status
     */
    public RetryTaskStatusEnum getStatus() {
        return status;
    }

    /**
     * Setter method for property status.
     *
     * @param status value to be assigned to property status
     */
    public void setStatus(RetryTaskStatusEnum status) {
        this.status = status;
    }

    /**
     * Getter method for property retryNo.
     *
     * @return property value of retryNo
     */
    public int getRetryNo() {
        return retryNo;
    }

    /**
     * Setter method for property retryNo.
     *
     * @param retryNo value to be assigned to property retryNo
     */
    public void setRetryNo(int retryNo) {
        this.retryNo = retryNo;
    }

    /**
     * Getter method for property memo.
     *
     * @return property value of memo
     */
    public String getMemo() {
        return memo;
    }

    /**
     * Setter method for property memo.
     *
     * @param memo value to be assigned to property memo
     */
    public void setMemo(String memo) {
        this.memo = memo;
    }

    /**
     * Getter method for property gmtCreate.
     *
     * @return property value of gmtCreate
     */
    public Date getGmtCreate() {
        return gmtCreate;
    }

    /**
     * Setter method for property gmtCreate.
     *
     * @param gmtCreate value to be assigned to property gmtCreate
     */
    public void setGmtCreate(Date gmtCreate) {
        this.gmtCreate = gmtCreate;
    }

    /**
     * Getter method for property gmtModified.
     *
     * @return property value of gmtModified
     */
    public Date getGmtModified() {
        return gmtModified;
    }

    /**
     * Setter method for property gmtModified.
     *
     * @param gmtModified value to be assigned to property gmtModified
     */
    public void setGmtModified(Date gmtModified) {
        this.gmtModified = gmtModified;
    }

    /**
     * Getter method for property scheduleTime.
     *
     * @return property value of scheduleTime
     */
    public Date getScheduleTime() {
        return scheduleTime;
    }

    /**
     * Setter method for property scheduleTime.
     *
     * @param scheduleTime value to be assigned to property scheduleTime
     */
    public void setScheduleTime(Date scheduleTime) {
        this.scheduleTime = scheduleTime;
    }

    /**
     * @see Object#equals(Object)
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        RetryTaskInfo taskInfo = (RetryTaskInfo) o;

        return id != null ? id.equals(taskInfo.id) : taskInfo.id == null;

    }

    /**
     * @see Object#hashCode()
     */
    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

    /**
     * @see Object#toString()
     */
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

}
